<template>
    <div class="app-container">
      <el-table v-loading="loading" :data="orderGoodsList" row-key="orderGoodsId" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" v-if="false"/>
        <el-table-column label="表id自增" align="center" prop="orderGoodsId" v-if="false"/>
        <el-table-column label="商品名称" align="center" prop="goodsName" />
        <el-table-column label="商品货号" align="center" prop="goodsSn" v-if="false"/>
        <el-table-column label="商品图片" align="center" prop="originalImg" v-if="false"/>
        <el-table-column label="购买数量" align="center" prop="goodsNum" />
        <el-table-column label="商品价" align="center" prop="goodsPrice" />
        <el-table-column label="会员折扣价" align="center" prop="memberGoodsPrice" v-if="false"/>
        <el-table-column label="对应瓶条码" align="center" prop="barCode" v-if="false"/>
        <el-table-column label="0未发货，1已发货，2已换货，3已退货" align="center" prop="isSend" v-if="false">
          <template #default="scope">
            <dict-tag :options="bl_shipping_status" :value="scope.row.isSend"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" v-if="false">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['bl:ordergoods:edit']">修改</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
  
      <!-- 添加或修改订单商品对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="OrderGoodsRef" :model="form" :rules="rules" label-width="80px">
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="orderGoods">
  import { listOrderGoods, getOrderGoods, delOrderGoods, addOrderGoods, updateOrderGoods } from "@/api/bl/order/orderGoods";
  
  const { proxy } = getCurrentInstance();
  const { bl_shipping_status } = proxy.useDict('bl_shipping_status');
  
  const orderGoodsList = ref([]);
  const open = ref(false);
  const loading = ref(true);
  const showSearch = ref(true);
  const ids = ref([]);
  const single = ref(true);
  const multiple = ref(true);
  const total = ref(0);
  const title = ref("");
  const route = useRoute();
  
  const data = reactive({
    form: {},
    queryParams: {
      pageNum: 1,
      pageSize: 10,
      orderId: null,
      deliveryOrderId: null,
    },
    rules: {
    }
  });
  
  const { queryParams, form, rules } = toRefs(data);
  
  /** 查询订单商品列表 */
  function getList() {
    queryParams.value.orderId = route.params && route.params.orderId;
    loading.value = true;
    listOrderGoods(queryParams.value).then(response => {
      orderGoodsList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }
  
  // 取消按钮
  function cancel() {
    open.value = false;
    reset();
  }
  
  // 表单重置
  function reset() {
    form.value = {
      orderGoodsId: null,
      orderId: null,
      deliveryOrderId: null,
      goodsId: null,
      goodsName: null,
      goodsSn: null,
      originalImg: null,
      goodsNum: null,
      goodsPrice: null,
      memberGoodsPrice: null,
      barCode: null,
      isSend: null,
      createBy: null,
      createTime: null,
      updateBy: null,
      updateTime: null
    };
    proxy.resetForm("OrderGoodsRef");
  }
  
  /** 搜索按钮操作 */
  function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
  }
  
  /** 重置按钮操作 */
  function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
  }
  
  // 多选框选中数据
  function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.orderGoodsId);
    single.value = selection.length != 1;
    multiple.value = !selection.length;
  }
  
  /** 新增按钮操作 */
  function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加订单商品";
  }
  
  /** 修改按钮操作 */
  function handleUpdate(row) {
    reset();
    const _orderGoodsId = row.orderGoodsId || ids.value
    getOrderGoods(_orderGoodsId).then(response => {
      form.value = response.data;
      open.value = true;
      title.value = "修改订单商品";
    });
  }
  
  /** 提交按钮 */
  function submitForm() {
    proxy.$refs["OrderGoodsRef"].validate(valid => {
      if (valid) {
        if (form.value.orderGoodsId != null) {
          updateOrderGoods(form.value).then(response => {
            proxy.$modal.msgSuccess("修改成功");
            open.value = false;
            getList();
          });
        } else {
          addOrderGoods(form.value).then(response => {
            proxy.$modal.msgSuccess("新增成功");
            open.value = false;
            getList();
          });
        }
      }
    });
  }
  
  /** 删除按钮操作 */
  function handleDelete(row) {
    const _orderGoodsIds = row.orderGoodsId || ids.value;
    proxy.$modal.confirm('是否确认删除订单商品编号为"' + _orderGoodsIds + '"的数据项？').then(function() {
      return delOrderGoods(_orderGoodsIds);
    }).then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  }
  
  /** 导出按钮操作 */
  function handleExport() {
    proxy.download('bl/OrderGoods/export', {
      ...queryParams.value
    }, `OrderGoods_${new Date().getTime()}.xlsx`)
  }
  
  getList();
  </script>
  